/**
 * Created by chenmingkang on 16/3/8.
 */

app.run(["$rootScope","$http","shareFactory",function($rootScope,$http,shareFactory) {
    $rootScope.$on('$stateChangeSuccess', function(evt, current, previous) {
        if(current.tabSelectIndex){   //tab选中样子
            $rootScope.targetScope = current.tabSelectIndex;
        }

        var url;
        if(location.href.indexOf('promote_code') > -1){
            url = location.href.replace(/promote_code=Izc(=)*/g,"promote_code="+ ($rootScope.userInfoData ? $rootScope.userInfoData.myPromoteCode : ''))
        }else{
            url = location.href + '?promote_code=' + ($rootScope.userInfoData ? $rootScope.userInfoData.myPromoteCode : '')
        }

        shareFactory({
            link : url
        });
    });

    if($rootScope.browser.v.weixin){
        //微信的分享
        $http({
            method:'get',
            url: servicePath + 'weixin/jsapi_config',
            cache: false,
            dataType: 'json',
            params:{
                "url" : location.href.split('#')[0]
            }
        }).success(function(data, status) {
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: data.data.appId, // 必填，公众号的唯一标识
                timestamp: data.data.timestamp, // 必填，生成签名的时间戳
                nonceStr: data.data.nonceStr, // 必填，生成签名的随机串
                signature: data.data.signature,// 必填，签名，见附录1
                jsApiList: ["onMenuShareAppMessage","onMenuShareTimeline","onMenuShareQQ","onMenuShareWeibo"] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            });
            wx.error(function(res){
                //alert(res);
            });
        });
    }

}]);
